package com.yth.utils.udf.function;

import com.yth.utils.udf.Udf;

import java.util.Arrays;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2024/12/18 14:44
 */
@SuppressWarnings("unused") // used by script
public class StringUtils implements Udf {

    private static final Integer[] EMPTY_INTEGER_ARRAY = new Integer[0];
    private static final Long[] EMPTY_LONG_ARRAY = new Long[0];
    private static final Boolean[] EMPTY_BOOLEAN_ARRAY = new Boolean[0];
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    public static Integer[] toIntegerArray(String str) {
        if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
            return EMPTY_INTEGER_ARRAY;
        }
        return Arrays.stream(str.split(","))
                .filter(org.apache.commons.lang3.StringUtils::isNotBlank)
                .map(org.apache.commons.lang3.StringUtils::trim)
                .map(Integer::parseInt)
                .toArray(Integer[]::new);
    }

    public static Long[] toLongArray(String str) {
        if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
            return EMPTY_LONG_ARRAY;
        }
        return Arrays.stream(str.split(","))
                .filter(org.apache.commons.lang3.StringUtils::isNotBlank)
                .map(org.apache.commons.lang3.StringUtils::trim)
                .map(Long::parseLong)
                .toArray(Long[]::new);
    }

    public static Boolean[] toBooleanArray(String str) {
        if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
            return EMPTY_BOOLEAN_ARRAY;
        }
        return Arrays.stream(str.split(","))
                .filter(org.apache.commons.lang3.StringUtils::isNotBlank)
                .map(org.apache.commons.lang3.StringUtils::trim)
                .map(Boolean::parseBoolean)
                .toArray(Boolean[]::new);
    }

    public static String[] toStringArray(String str) {
        if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
            return EMPTY_STRING_ARRAY;
        }
        return Arrays.stream(str.split(","))
                .filter(org.apache.commons.lang3.StringUtils::isNotBlank)
                .map(org.apache.commons.lang3.StringUtils::trim)
                .toArray(String[]::new);
    }

    private StringUtils() {
    }
}
